onOpened = function() {
	console.log("onOpened");
}

onMessage = function(message) {
	$.each(channelCallback.onMessage, function(index, callback) { 
		callback(eval("("+message.data+")"));
	});
}

onError = function(error) {
	console.log("onError : "+ error.description );
}

onClose = function() {
	console.log("onClose");
}

sendMessage = function(channelId, message) {
	var message = { 'channelId' : channelId, 'message' : message };
	$.post('channel/message',  {'json':JSON.stringify(message)}, function(data) {
	});
}

channelCallback = {'onMessage' : []};

channelConnect = function(id){
	$.getJSON('channel/token?id='+id, function(token) {
	    var channel = new goog.appengine.Channel(token);
	    var socket = channel.open();
	    socket.onopen = onOpened;
	    socket.onmessage = onMessage;
	    socket.onerror = onError;
	    socket.onclose = onClose;
	});
}

$(function() {
	channelCallback.onMessage.push(function(message){
		console.log("onMessage : " + message);
	})
});